package com.wephone.Student.ui;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.wephone.Student.R;
import com.wephone.Student.bean.BaseResponse;
import com.wephone.Student.bean.data.ZhidaoAskData;
import com.wephone.Student.bean.data.ZhidaoCommentData;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.RoundImageView;

public class ZhidaoAnswerDetailActivity extends BaseActivity {
	private TextView titleDetailText;
	private TextView topTitleText;
	private ImageView titleStatusImage;
	private ZhidaoCommentData commentData;
	private ZhidaoAskData askData;
	private int updateStatus = -1;
	private ImageView detailImage;
	private boolean isThank = false;
	private boolean isHelp = false;
	private boolean isFavor = false;
	private boolean isCommiting = false;
	
	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(ZhidaoAnswerDetailActivity.this,BaseResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);

				if(getDataSuccess){
//					System.out.println("-----updateStatus-----"+updateStatus);
					if(updateStatus==1){
						if(isThank){
							((ImageView)findViewById(R.id.bottom_btn_1)).setImageResource(R.drawable.btn_thanks_default);
							Toast.makeText(getApplicationContext(), "撤销感谢作者", Toast.LENGTH_SHORT).show();
						}else{
							((ImageView)findViewById(R.id.bottom_btn_1)).setImageResource(R.drawable.btn_thanks_focused);
							Toast.makeText(getApplicationContext(), "感谢作者成功", Toast.LENGTH_SHORT).show();
						}
						isThank = !isThank;
						setResult(22);
					}else if(updateStatus==2){
						if(isHelp){
							((ImageView)findViewById(R.id.bottom_btn_3)).setImageResource(R.drawable.btn_zhidao_detail_help_default);
							Toast.makeText(getApplicationContext(), "撤销没有帮助", Toast.LENGTH_SHORT).show();
						}else{
							((ImageView)findViewById(R.id.bottom_btn_3)).setImageResource(R.drawable.btn_zhidao_detail_help_focused);
							Toast.makeText(getApplicationContext(), "没有帮助已提交", Toast.LENGTH_SHORT).show();
						}
						isHelp = !isHelp;
						setResult(22);
					}else if(updateStatus==3){
						if(isFavor){
							((ImageView)findViewById(R.id.bottom_btn_4)).setImageResource(R.drawable.btn_zhidao_detail_favor_default);
							Toast.makeText(getApplicationContext(), "撤销收藏成功", Toast.LENGTH_SHORT).show();
						}else{
							((ImageView)findViewById(R.id.bottom_btn_4)).setImageResource(R.drawable.btn_zhidao_detail_favor_focused);
							Toast.makeText(getApplicationContext(), "加入收藏成功", Toast.LENGTH_SHORT).show();
						}
						isFavor = !isFavor;
						setResult(22);
					}
				}
				isCommiting = false;
			}
		};
		return handler;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode==3 && resultCode>0){
			setResult(22);
			commentData.setReplynum(""+resultCode);
			((TextView)findViewById(R.id.bottom_textview_2)).setText("评论("+resultCode+")");
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zhidao_answer_detail);
		
		Bundle bundle = getIntent().getExtras();
		if(bundle!=null){
			askData = (ZhidaoAskData)bundle.getSerializable("askData");
			commentData = (ZhidaoCommentData)bundle.getSerializable("commentData");
			
			init();
		}
		
	}
	
	private void init(){
		topTitleText = (TextView)findViewById(R.id.top_bar_title_text);
		titleDetailText = (TextView)findViewById(R.id.title_detail_text);
		titleStatusImage = (ImageView)findViewById(R.id.title_status_image);
		detailImage = (ImageView)findViewById(R.id.zhidao_detail_pic_image);
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		topTitleText.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(titleDetailText.getVisibility()==View.VISIBLE){
					titleDetailText.setVisibility(View.GONE);
					titleStatusImage.setImageResource(R.drawable.btn_moreinfo_closed);
				}else{
					titleDetailText.setVisibility(View.VISIBLE);
					titleStatusImage.setImageResource(R.drawable.btn_moreinfo_open);
				}
			}
		});
		findViewById(R.id.bottom_btn_2).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Bundle bundle = new Bundle();
				bundle.putString("qId", askData.getId());
				bundle.putString("cId", commentData.getId());
				startActivityForResult(new Intent(getApplicationContext(),ZhidaoCommentListActivity.class).putExtras(bundle),3);
			}
		});
//		System.out.println("-----thank-----"+commentData.getThank());
		if(commentData.getThank()!=null && commentData.getThank().contains(BaseActivity.getMyInfo(getApplicationContext()).getuId())){
			isThank = true;
			((ImageView)findViewById(R.id.bottom_btn_1)).setImageResource(R.drawable.btn_thanks_focused);
		}
		findViewById(R.id.bottom_btn_1).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(isCommiting){
					return;
				}
				isCommiting = true;
				Toast.makeText(getApplicationContext(), "正在提交", Toast.LENGTH_SHORT).show();
				
				updateStatus = 1;
				RequestParams params = new RequestParams();
				params.put("id", commentData.getId());
				params.put("uid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
				params.put("key", "thank");
				params.put("val", isThank?"0":"1");
				HttpClient.getInstance().get(Constants.API_UPDATE_ZHIDAO_COMMENT_URL, params, getHandler());
			}
		});
		((TextView)findViewById(R.id.bottom_textview_2)).setText("评论("+commentData.getReplynum()+")");
		if(commentData.getHelp()!=null && commentData.getHelp().contains(BaseActivity.getMyInfo(getApplicationContext()).getuId())){
			isHelp = true;
			((ImageView)findViewById(R.id.bottom_btn_3)).setImageResource(R.drawable.btn_zhidao_detail_help_focused);
		}
		findViewById(R.id.bottom_btn_3).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(isCommiting){
					return;
				}
				isCommiting = true;
				Toast.makeText(getApplicationContext(), "正在提交", Toast.LENGTH_SHORT).show();
				
				updateStatus = 2;
				RequestParams params = new RequestParams();
				params.put("id", commentData.getId());
				params.put("uid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
				params.put("key", "help");
				params.put("val", isHelp?"0":"1");
				HttpClient.getInstance().get(Constants.API_UPDATE_ZHIDAO_COMMENT_URL, params, getHandler());
			}
		});
		if(commentData.getCollect()!=null && commentData.getCollect().contains(BaseActivity.getMyInfo(getApplicationContext()).getuId())){
			isFavor = true;
			((ImageView)findViewById(R.id.bottom_btn_4)).setImageResource(R.drawable.btn_zhidao_detail_favor_focused);
		}
		findViewById(R.id.bottom_btn_4).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(isCommiting){
					return;
				}
				isCommiting = true;
				Toast.makeText(getApplicationContext(), "正在提交", Toast.LENGTH_SHORT).show();
				
				updateStatus = 3;
				RequestParams params = new RequestParams();
				params.put("id", commentData.getId());
				params.put("uid", BaseActivity.getMyInfo(getApplicationContext()).getuId());
				params.put("key", "collect");
				params.put("val", isFavor?"0":"1");
				HttpClient.getInstance().get(Constants.API_UPDATE_ZHIDAO_COMMENT_URL, params, getHandler());
			}
		});
		
		((TextView)findViewById(R.id.top_bar_title_text)).setText(askData.getTitle());
		titleDetailText.setText(askData.getTitle());
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
		TextView itemName = (TextView)findViewById(R.id.item_answer_name);
		RoundImageView itemImage = (RoundImageView)findViewById(R.id.item_image);
		if(commentData.getAnonymous()!=null && commentData.getAnonymous().equals("1")){
			itemImage.setImageResource(R.drawable.default_user_logo);
			itemName.setText("匿名");
		}else{
			itemName.setText(commentData.getuInfo().getuName());
			if(!TextUtils.isEmpty(commentData.getuInfo().getuIcon())){
				DisplayImageOptions options = new DisplayImageOptions.Builder()
		        .showStubImage(R.drawable.default_user_logo)
		        .showImageOnFail(R.drawable.default_user_logo)
		        .bitmapConfig(Bitmap.Config.ARGB_8888)
		        .cacheInMemory()
		        .cacheOnDisc()
//		        .displayer(new RoundedBitmapDisplayer(1000))
		        .build();
				String endStr = commentData.getuInfo().getuIcon().substring(commentData.getuInfo().getuIcon().lastIndexOf("."));
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+commentData.getuInfo().getuIcon().replace(endStr, "_150_150"+endStr), itemImage, options);
				
				itemImage.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						Bundle bundle = new Bundle();
						bundle.putSerializable("userData", commentData.getuInfo());
						startActivity(new Intent(getApplicationContext(),UserInfoDetailActivity.class).putExtras(bundle));
					}
				});
			}else{
				itemImage.setImageResource(R.drawable.default_user_logo);
			}
		}
		TextView itemTime = (TextView)findViewById(R.id.item_answer_time);
		itemTime.setText(sdf.format(new Date(Long.parseLong(commentData.getCtime())*1000)));
		TextView itemContent = (TextView)findViewById(R.id.zhidao_detail_content);
		itemContent.setText(commentData.getContent());
		if(commentData.getPictures()!=null && commentData.getPictures().length>0){
			if(!TextUtils.isEmpty(commentData.getPictures()[0])){
				DisplayImageOptions options = new DisplayImageOptions.Builder()
		        .showStubImage(R.drawable.default_image)
		        .showImageOnFail(R.drawable.default_image)
		        .bitmapConfig(Bitmap.Config.ARGB_8888)
		        .cacheInMemory()
		        .cacheOnDisc()
		        .displayer(new RoundedBitmapDisplayer(0))
		        .build();
				imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+commentData.getPictures()[0], detailImage, options);
				
				detailImage.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						String bigImgUrl = Constants.IMAGE_PHOTO_SERVER_PATH+commentData.getPictures()[0]; 
						startActivity(new Intent(getApplicationContext(),GalleryUrlActivity.class).putExtra("imgUrl", bigImgUrl));
					}
				});
			}
		}
		
	}

}
